package com.xbfinance.manager.service.impl;

import com.xbfinance.common.util.JedisClient;
import com.xbfinance.common.util.JsonUtils;
import com.xbfinance.common.util.StrKit;
import com.xbfinance.manager.dao.BannerPicMapper;
import com.xbfinance.manager.pojo.po.BannerPic;
import com.xbfinance.manager.pojo.po.BannerPicExample;
import com.xbfinance.manager.service.BannerPicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional
public class BannerPicServiceImpl implements BannerPicService {
    @Autowired
    private BannerPicMapper bannerPicDao;
    private static final String PICURL = "PICURL";
    @Autowired
    private JedisClient jedis;
    @Override
    public List<String> listPicUrl(Integer cid) {
        List<String> picList = new ArrayList<>();
        String picListJson = jedis.hget(PICURL, cid+"");
        if (StrKit.notBlank(picListJson)) {
            picList = JsonUtils.jsonToList(picListJson,String.class);
            return picList;
        }

        BannerPicExample example=new BannerPicExample();
        example.createCriteria()
                .andCidEqualTo(cid);
        List<BannerPic> bannerPics = bannerPicDao.selectByExample(example);
        for (BannerPic bannerPic : bannerPics) {
            picList.add(bannerPic.getUrl());
        }
        jedis.hset(PICURL, cid+"", JsonUtils.objectToJson(picList));
        return picList;
    }
}
